﻿#include "OptionsPanel.h"
#include <QLabel>
#include <QGroupBox>
#include <QSlider>

#include <QFileDialog>   //保存文件
#include <QMessageBox>


OptionsPanel::OptionsPanel(ConfigUI* conf, QWidget* parent) : QWidget(parent) {
	ui.setupUi(this);
	m_config = conf;
	styleInit();
	setPanelUI();
	setup_panelSlots();
}

OptionsPanel::~OptionsPanel() {}

void OptionsPanel::styleInit() {
	
}

void OptionsPanel::setPanelUI() {
	//后处理模式
	ui.PostMode->addItem("None", 0);
	ui.PostMode->addItem("Off", 1);
	ui.PostMode->addItem("Minimal", 2);
	ui.PostMode->addItem("Smooth", 3);
	ui.PostMode->addItem("Full", 4);

	//网格类型: 0无网格，1线形网格，2点形网格
    ui.showGridType->addItem("No Grids", 0);
	ui.showGridType->addItem("Line Grids", 1);
	ui.showGridType->addItem("Point Grids", 2);
}
void OptionsPanel::setup_panelSlots() {
	//1.按钮信号
	connect(ui.openResBt, &QPushButton::clicked, this, [this]() {			// 打开Ren文件，用于查看
		emit open_res_request_signal();
		});
	connect(ui.resetBt, &QPushButton::clicked, this, [this]() {				// 复位重置
		emit reset_request_signal();
		});
	connect(ui.randPinBt, &QPushButton::clicked, this, [this]() {			// 随机pin
		emit random_pin_request_signal();
		});
	connect(ui.pinDecreaseBt, &QPushButton::clicked, this, [this]() {		// 减少pin
		emit pin_decrease_request_signal();
		});
	connect(ui.pinIncreaseBt, &QPushButton::clicked, this, [this]() {		// 增加pin
		emit pin_increase_request_signal();
		});
	connect(ui.saveCaseBt, &QPushButton::clicked, this, [this]() {			// 保存case
		emit save_case_request_signal();
		});
	connect(ui.saveImageBt, &QPushButton::clicked, this, [this]() {			// 保存图片
		emit save_image_request_signal();
		});
	connect(ui.writePreBt, &QPushButton::clicked, this, [this]() {			// 保存预处理结果
		emit write_pre_request_signal();
		});
	connect(ui.addResBt, &QPushButton::clicked, this, [this]() {			// 追加保存结果
		emit write_res_request_signal();
		});
	connect(ui.PPDTRunBt, &QPushButton::clicked, this, [this]() {			// 运行PPDT寻路算法
		emit PPTT_run_request_signal();
		});
	connect(ui.polyRunBt, &QPushButton::clicked, this, [this]() {			// 运行Polygon寻路算法
		emit poly_run_request_signal();
		});

	//2.显示选项
	ui.checkFlipTD->setChecked(m_config->m_flip_up_down);
	connect(ui.checkFlipTD, &QCheckBox::checkStateChanged, this, [this]() {			// 上下翻转
		m_config->m_flip_up_down = ui.checkFlipTD->checkState() == Qt::Checked;
		paintChanged(1);
		});
	ui.checkFlipLR->setChecked(m_config->m_flip_left_right);
	connect(ui.checkFlipLR, &QCheckBox::checkStateChanged, this, [this]() {			// 左右翻转
		m_config->m_flip_left_right = ui.checkFlipLR->checkState() == Qt::Checked;
		paintChanged(1);
		});
	ui.checkRotate90->setChecked(m_config->m_rotate_90);
	connect(ui.checkRotate90, &QCheckBox::checkStateChanged, this, [this]() {		// 旋转90度
		m_config->m_rotate_90 = ui.checkRotate90->checkState() == Qt::Checked;
		paintChanged(1);
		});
	ui.checkShowFlyLines->setChecked(m_config->m_showFlyLines);
	connect(ui.checkShowFlyLines, &QCheckBox::toggled, this, [this](bool checked) {	// 显示飞线
		m_config->m_showFlyLines = checked;
		paintChanged(0);
		});
	ui.checkShowWires->setChecked(m_config->m_showWires);
	connect(ui.checkShowWires, &QCheckBox::checkStateChanged, this, [this]() {		// 显示走线结果
		m_config->m_showWires = ui.checkShowWires->checkState() == Qt::Checked;
		paintChanged(0);
		});
	ui.checkShowObs->setChecked(m_config->m_showObss);
	connect(ui.checkShowObs, &QCheckBox::checkStateChanged, this, [this]() {		// 显示障碍物
		m_config->m_showObss = ui.checkShowObs->checkState() == Qt::Checked;
		paintChanged(0);
		});
	ui.checkShowCorners->setChecked(m_config->m_showCorners);
	connect(ui.checkShowCorners, &QCheckBox::checkStateChanged, this, [this]() {	// 显示规划点
		m_config->m_showCorners = ui.checkShowCorners->checkState() == Qt::Checked;
		paintChanged(0);
		});
	ui.checkShowPathPts->setChecked(m_config->m_showPathPts);
	connect(ui.checkShowPathPts, &QCheckBox::checkStateChanged, this, [this]() {	// 显示路径点
		m_config->m_showPathPts = ui.checkShowPathPts->checkState() == Qt::Checked;
		paintChanged(0);
		});
	ui.checkShowResPts->setChecked(m_config->m_show_resPts);
	connect(ui.checkShowResPts, &QCheckBox::checkStateChanged, this, [this]() {		// 显示Res点
		m_config->m_show_resPts = ui.checkShowResPts->checkState() == Qt::Checked;
		paintChanged(0);
		});
	ui.checkOnGrid->setChecked(m_config->m_onGrids);
	connect(ui.checkOnGrid, &QCheckBox::checkStateChanged, this, [this]() {			// 在网格上
		m_config->m_onGrids = ui.checkOnGrid->checkState() == Qt::Checked;
		paintChanged(0);
		});
	ui.gridSizeEdit->setText(QString::number(m_config->m_gridSize, 'f', 3));		// 网格大小
	connect(ui.gridSizeEdit, &QLineEdit::textChanged, this, [this](const QString& text) {
		bool ok;
		double value = text.toDouble(&ok);
		if (ok) {
			m_config->m_gridSize = value;
			paintChanged(0);
		}
		});
	ui.showGridType->setCurrentIndex(m_config->m_gridType);								// 网格显示类型选择
	connect(ui.showGridType, QOverload<int>::of(&QComboBox::currentIndexChanged), this, [this](int index) {
		m_config->m_gridType = index;
		paintChanged(0);
		});
	
	ui.checkPostProcess->setChecked(m_config->m_postOn);
	connect(ui.checkPostProcess, &QCheckBox::checkStateChanged, this, [this]() {		// 启用后处理
		m_config->m_postOn = ui.checkPostProcess->checkState() == Qt::Checked;
		paintChanged(0);
		});
	ui.PostMode->setCurrentIndex(m_config->m_postMode);									// 后处理模式选择
	connect(ui.PostMode, QOverload<int>::of(&QComboBox::currentIndexChanged), this, [this](int index) {
		m_config->m_postMode = index;
		paintChanged(0);
		});
}
